2 * Copyright (c) 2013-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 #import "KNPersistentState.h"
27 @implementation KNPersistentState
29 -(NSURL*)urlForStorage
31 return [NSURL URLWithString:@"Preferences/com.apple.security.KCN.plist" relativeToURL:[[NSFileManager defaultManager] URLForDirectory:NSLibraryDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil]];
34 +(instancetype)loadFromStorage
36 KNPersistentState *state = [KNPersistentState new];
41 id plist = @{@"lastWritten": [NSDate distantPast]};
44 NSData *stateData = [NSData dataWithContentsOfURL:[state urlForStorage] options:0 error:&error];
46 NSLog(@"Can't read state data (p=%@, err=%@)", [state urlForStorage], error);
48 NSPropertyListFormat format;
49 plist = [NSPropertyListSerialization propertyListWithData:stateData options: NSPropertyListMutableContainersAndLeaves format:&format error:&error];
52 NSLog(@"Can't deserialize %@, e=%@", stateData, error);
56 state.lastCircleStatus = plist[@"lastCircleStatus"] ? [plist[@"lastCircleStatus"] intValue] : kSOSCCCircleAbsent;
57 state.lastWritten = plist[@"lastWritten"];
58 state.pendingApplicationReminder = plist[@"pendingApplicationReminder"] ?: [NSDate distantFuture];
59 state.applicationDate = plist[@"applicationDate"] ?: [NSDate distantPast];
60 state.debugLeftReason = plist[@"debugLeftReason"];
61 state.pendingApplicationReminderInterval = plist[@"pendingApplicationReminderInterval"];
62 state.absentCircleWithNoReason = plist[@"absentCircleWithNoReason"] ? [plist[@"absentCircleWithNoReason"] intValue] : NO;
64 if (!state.pendingApplicationReminderInterval || [state.pendingApplicationReminderInterval doubleValue] <= 0) {
65 state.pendingApplicationReminderInterval = [NSNumber numberWithUnsignedInt: 24*60*60];
73 NSMutableDictionary *plist = [@{@"lastCircleStatus" : [NSNumber numberWithInt:self.lastCircleStatus],
74 @"lastWritten" : [NSDate date],
75 @"applicationDate" : self.applicationDate,
76 @"pendingApplicationReminder" : self.pendingApplicationReminder,
77 @"pendingApplicationReminderInterval": self.pendingApplicationReminderInterval,
78 @"absentCircleWithNoReason" : [NSNumber numberWithBool:self.absentCircleWithNoReason],
80 if (self.debugLeftReason)
81 plist[@"debugLeftReason"] = self.debugLeftReason;
82 NSLog(@"writeToStorage plist=%@", plist);
85 NSData *stateData = [NSPropertyListSerialization dataWithPropertyList:plist format:NSPropertyListXMLFormat_v1_0 options:kCFPropertyListImmutable error:&error];
87 NSLog(@"Can't serialize %@: %@", plist, error);
90 if (![stateData writeToURL:[self urlForStorage] options:NSDataWritingAtomic error:&error]) {
91 NSLog(@"Can't write to %@, error=%@", [self urlForStorage], error);